package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.provider.mapper.MediaAgentBindMapper;
import com.weixing.mall.provider.model.domain.MediaAgentBind;
import com.weixing.mall.provider.model.dto.ads.MediaAgentBindDto;
import com.weixing.mall.provider.model.query.MediaAgentBindQuery;
import com.weixing.mall.provider.model.vo.ads.MediaAgentBindVo;
import com.weixing.mall.provider.service.IMediaAgentBindService;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 广告模块-广告专员与代理商绑定关系表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class MediaAgentBindServiceImpl extends ServiceImpl<MediaAgentBindMapper, MediaAgentBind> implements IMediaAgentBindService {


    @Override
    public IPage selectOfPage(MediaAgentBindQuery mediaAgentBindQuery, Page initMpPage) {
        return baseMapper.selectOfPage(initMpPage,mediaAgentBindQuery);
    }

    @Override
    public void save(MediaAgentBindDto mediaAgentBindDto) {
        if(mediaAgentBindDto.getAgent() != null && checkExist(mediaAgentBindDto.getAgent())){
            throw new BusinessException("不可重复绑定");
        }
        MediaAgentBind mediaAgentBind = new MediaAgentBind();
        BeanUtils.copyProperties(mediaAgentBindDto,mediaAgentBind);
        if(null ==mediaAgentBindDto.getId()){
            save(mediaAgentBind);
        }else {
            updateById(mediaAgentBind);
        }
    }

    private boolean checkExist(Long agentId){
        return count(new LambdaQueryWrapper<MediaAgentBind>().eq(MediaAgentBind::getAgent,agentId))>0?true:false;
    }

    @Override
    public List<MediaAgentBindVo> selectOfPage(MediaAgentBindQuery mediaAgentBindQuery) {
        return baseMapper.selectOfPage(mediaAgentBindQuery);
    }
}
